grep 满足 或 排除多个关键字 您所在的位置:网站首页 linux grep -v空行 grep 满足 或 排除多个关键字

grep 满足 或 排除多个关键字

2024-01-23 19:33| 来源: 网络整理| 查看: 265

grep 同时满足多个关键字

① grep -E "word1|word2|word3" file.txt 满足任意条件(word1、word2和word3之一)将匹配。 ② grep word1 file.txt | grep word2 |grep word3 必须同时满足三个条件(word1、word2和word3)才匹配。

grep 同时排除多个关键字

不说废话, 例如需要排除 abc.txt 中的 mmm nnn

grep -v 'mmm\|nnn' abc.txt

再举个例子,需要确定mac 的本机ip地址, 显然直接可以输入 ifconfig, 但是会出来一大堆信息,那么再通过 grep inet 可以拿到类似如下的信息:

bash-3.2# ifconfig | grep inet    inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 inet6 fe80::c37:dee4:5ad4:944b%en0 prefixlen 64 secured scopeid 0x4 inet 10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255 inet6 fe80::8dc:a3ff:feaf:fbe1%awdl0 prefixlen 64 scopeid 0x9 inet6 fe80::bd0c:5502:92ad:25e1%utun0 prefixlen 64 scopeid 0xa

复制代码

但是这样还是很多,需要从这几条信息里面去找到所需要的 ip 地址,我们可能想到了使用 grep -v 屏蔽掉 inet6,结果如下:

bash-3.2# ifconfig | grep inet | grep -v inet6 inet 127.0.0.1 netmask 0xff000000 inet 10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255

这样其实也可以看了,但是强迫症患者伤不起啊,我就只要一条,怎么弄呢,显然还可以继续通过 grep -v 127.0.0.1 来屏蔽掉第一条记录,如下:

bash-3.2# ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 inet 10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255

这样好像不是很优雅,那就刚刚说的方法,如下:

bash-3.2# ifconfig | grep inet | grep -v 'inet6\|127.0.0.1' inet 10.60.104.38 netmask 0xfffffe00 broadcast 10.60.105.255


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有